package com.foodmarket.mapper;

import com.foodmarket.pojo.Orders;
import com.foodmarket.pojo.buyerOrders;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface OrderMapper {


    @Select("select  b.address, b.username, f.food_name,o.quantity,  o.order_date from" +
            " Sellers s JOIN FoodItems f ON s.seller_id = f.seller_id JOIN Orders o ON f.food_id = o.food_id" +
            " JOIN Buyers b ON o.buyer_id = b.buyer_id where  s.seller_id = #{sellerId}")
    List<Orders> findAllFood(int sellerId);


    @Insert("insert into orders(buyer_id,food_id,quantity)" +
            "values(#{buyerId},#{foodId},#{quantity})")
    void addOrder(Orders orders);

    @Delete("DELETE FROM orders WHERE buyer_id=#{buyerId}")
    void deleteOrder(Integer buyerId);


    @Select("SELECT\n" +
            "    s.username,\n" +
            "    s.phone,\n" +
            "    s.store_name,\n" +
            "    f.food_name,\n" +
            "    o.order_date,\n"+
            "    o.quantity,\n" +
            "    (f.food_price * o.quantity) AS totalPrice\n" +
            "FROM\n" +
            "    orders o\n" +
            "        JOIN\n" +
            "    fooditems f ON o.food_id = f.food_id\n" +
            "        JOIN\n" +
            "    sellers s ON f.seller_id = s.seller_id\n" +
            "WHERE\n" +
            "    o.buyer_id = #{buyerId};")
    List<buyerOrders> findBuyersOrders(int buyerId);

}
